package com.scs.application.modules.upms.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.scs.application.consts.GlobalConsts;
import com.scs.application.core.model.DataEntity;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.mvc.validation.constraints.Mobile;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;

/**
 * ${DESCRIPTION}
 */
@Data
@Accessors(chain = true)
@TableName("gen_table_column")
public class GenTableColumn extends DataEntity implements Serializable {

    private String tableId;

    private String tableName;

    private String columnName;

    private Integer columnSort;

    private String columnType;

    private String comments;

    private String attrName;

    private String attrType;

    private Short isPk;

    private Short isNull;

    private Short isInsert;

    private Short isUpdate;

    private Short isList;

    private Short isQuery;

    private String queryType;


    private Short isEdit;


    private String showType;

    private String colSpan;

    private String dictType;

    private JSONArray validators;

    private Short isNewRow;

    /**
     * 是否是基类字段
     *
     * @return
     */

    @JsonIgnore
    @JSONField(deserialize = false, serialize = false)
    public Boolean getIsNotBaseField() {
        // id 在 DataEntity 中没有定义
        return !StringUtils.equals(getAttrName(), "id")
                && !StringUtils.equals(getAttrName(), "parentId")
                && !StringUtils.equals(getAttrName(), "parentIds")
                && !StringUtils.equals(getAttrName(), "remarks")
                && !StringUtils.equals(getAttrName(), "creator")
                && !StringUtils.equals(getAttrName(), "gmtCreate")
                && !StringUtils.equals(getAttrName(), "modifier")
                && !StringUtils.equals(getAttrName(), "gmtModified")
                && !StringUtils.equals(getAttrName(), "delFlag");
    }

    /**
     * 获取字符串长度
     *
     * @return
     */
    @JsonIgnore
    @JSONField(deserialize = false, serialize = false)
    public String getDataLength() {
        String[] ss = StringUtils.split(StringUtils.substringBetween(StringUtils.lowerCase(columnType), "(", ")"), ",");
        if (ss != null && ss.length == 1) {// && "String".equals(getJavaType())){
            return ss[0];
        }
        return "0";
    }


    /**
     * 获取列注解列表
     *
     * @return
     */
    @JsonIgnore
    @JSONField(deserialize = false, serialize = false)
    public List<String> getAnnotationList() {
        List<String> list = Lists.newArrayList();


        // 导入Jackson注解
        if ("This".equals(attrType)) {
            list.add("com.fasterxml.jackson.annotation.JsonBackReference");
        }

        short isNull = getIsNull() == null ? GlobalConsts.YES : getIsNull();

        // 导入JSR303验证依赖包
        if (GlobalConsts.YES != isNull && !"String".equals(attrType)) {
            list.add("javax.validation.constraints.NotNull(message = \"" + comments + "不能为空\")");
        } else if (GlobalConsts.YES != isNull && "String".equals(attrType) && !"0".equals(getDataLength())) {
            list.add("javax.validation.constraints.NotBlank(message = \"" + comments + "不能为空\")");
            list.add("org.hibernate.validator.constraints.Length(min = 1, max = " + getDataLength()
                    + ", message = \"" + comments + "长度必须介于 1 和 " + getDataLength() + " 之间\")");
        } else if ("String".equals(attrType) && !"0".equals(getDataLength())) {
            list.add("org.hibernate.validator.constraints.Length(max = " + getDataLength()
                    + ", message = \"" + comments + "长度必须介于 0 和 " + getDataLength() + " 之间\")");
        }

        if (this.getValidators().contains("phone")) {
            // 手机号验证
            list.add(Mobile.class.getName() + "(message=\"" + comments + "格式错误\")");
        }
        return list;
    }

    /**
     * 获取简写列注解列表
     *
     * @return
     */
    @JsonIgnore
    @JSONField(deserialize = false, serialize = false)
    public List<String> getSimpleAnnotationList() {
        List<String> list = Lists.newArrayList();
        for (String ann : getAnnotationList()) {
            list.add(StringUtils.substringAfterLast(ann, "."));
        }
        return list;
    }

    /**
     * 字段的表单类型是否是组件类型。例如：仓库选择、物料选择等组件
     *
     * @return
     */
    @JsonIgnore
    @JSONField(deserialize = false, serialize = false)
    public Short getIsComponentType() {

        List<String> list = Lists.newArrayList();
        return list.contains(this.showType) ? GlobalConsts.YES : GlobalConsts.NO;
    }
}
